<?php

/**
 * blog actions.
 *
 * @package    lezgro
 * @subpackage blog
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class blogActions extends sfActions {

    public function executeIndex(sfWebRequest $request) {
        $this->articles = Doctrine_Core::getTable('Article')->getI18nArticles();
        $this->bg_class = 'bg_blog';
        $this->page_title = 'Lezgro - Blog';
        if ($request->isXmlHttpRequest()) {
            $response = array();
            $response['html'] = $this->getPartial('index', array('articles' => $this->articles));
            $response['title'] = $this->page_title;
            $response['bg_class'] = $this->bg_class;
            return $this->renderText(json_encode($response));
        }
        $this->getResponse()->setTitle($this->page_title);
    }

    public function executeShow(sfWebRequest $request) {
        $this->article = Doctrine_Core::getTable('Article')->findOneBySlug(array($request->getParameter('slug')));
        $this->forward404Unless($this->article);
        $this->bg_class = 'bg_blog-read';
        $this->page_title = 'Lezgro-Blog';
        if ($request->isXmlHttpRequest()) {
            $response = array();
            $response['html'] = $this->getPartial('show', array('article' => $this->article));
            $response['title'] = $this->page_title;
            $response['bg_class'] = $this->bg_class;
            return $this->renderText(json_encode($response));
        }
        $this->getResponse()->setTitle($this->page_title);
    }

    public function executeShowComments(sfWebRequest $request) {
        $this->article = Doctrine_Core::getTable('Article')->findOneBySlug(array($request->getParameter('slug')));
        $this->forward404Unless($this->article);
        $this->comments = $this->article->getOrderedComments();
        $this->bg_class = 'bg_blog-comments';
        $this->page_title = 'Lezgro-Blog-Comments';
        if ($request->isXmlHttpRequest()) {
            $response = array();
            $response['html'] = $this->getPartial('show_comments', array('comments' => $this->comments,'article'=>$this->article));
            $response['title'] = $this->page_title;
            $response['bg_class'] = $this->bg_class;
            return $this->renderText(json_encode($response));
        }
        $this->getResponse()->setTitle($this->page_title);
    }
    public function executeLoadCommentForm(sfWebRequest $request){
        sfContext::getInstance()->getConfiguration()->loadHelpers('I18N');
        $this->article = Doctrine_Core::getTable('Article')->findOneBySlug(array($request->getParameter('slug')));
        $this->forward404Unless($this->article);
         $this->bg_class = 'bg_blog-comments';
        $this->page_title = 'Lezgro - Blog-Comments';
        $this->form=new CommentForm();
        $this->form->setDefault('article_id', $this->article->getId());
        $this->form->setDefault('text',__('напишите ваш комментарий:'));
        if(sfContext::getInstance()->getUser()->getGuardUser()){
            $user=$this->getUser()->getUserProfile();
            $this->form->setDefault('user_name', $user->getFirstName().' '.$user->getLastName());
            $this->form->setDefault('email', $user->getSfGuardUser()->getEmailAddress());
        }else{
        $this->form->setDefault('user_name',__('Представтесь:'));
        $this->form->setDefault('email', 'e-mail:');
        }
        if ($request->isXmlHttpRequest()) {
            $response = array();
            $response['html'] = $this->getPartial('comment_form', array('article' => $this->article,'form'=>$this->form));
            $response['title'] = $this->page_title;
            $response['bg_class'] = $this->bg_class;
            return $this->renderText(json_encode($response));
        }
        $this->getResponse()->setTitle($this->page_title);
    }
    public function executeCreateComment(sfWebRequest $request) {
        $form = new CommentForm();
        sfContext::getInstance()->getConfiguration()->loadHelpers('URL');
         $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            $comment = $form->save();
        }
        $articleId=$form->getValue('article_id');
        if ($comment) {
            $this->article = Doctrine_Core::getTable('Article')->findOneById($articleId);
            $this->article->addNewComment($comment);
            $this->article = Doctrine_Core::getTable('Article')->findOneById($articleId);
            $this->forward404Unless($this->article);
            $this->comments = $this->article->getOrderedComments();
            $this->bg_class = 'bg_blog-comments';
            $this->page_title = 'Lezgro-Blog-Comments';
                $response = array();
                $response['html'] = $this->getPartial('show_comments', array('comments' => $this->comments, 'article' => $this->article));
                $response['title'] = $this->page_title;
                $response['bg_class'] = $this->bg_class;
                $response['url']=url_for(array('module'=>'blog','action'=>'showComments','slug'=>$this->article->getSlug()));
                return $this->renderText(json_encode($response));
        }
        $response['status']=false;
        $response['errors']=$form->processErrorsForAjaxResponse($response);
        return $this->renderText(json_encode($response));
    }
    protected function processForm(sfWebRequest $request, sfForm $form) {
        $form->bind(
                $request->getParameter($form->getName()), $request->getFiles($form->getName())
        );

        if ($form->isValid()) {
            $item = $form->save();

            return $item;
        }
        return false;
    }

}
